package com.lyn.frp;

import com.lyn.frp.config.FrpConfig;
import com.lyn.frp.listener.FrpListener;
import frpclib.Frpclib;

/**
 * @author longyn 2024/02/18
 * @version 1.0.0
 */
public class XFrp {

    private static final class XFrpTypeClass {
        private static final XFrp in = new XFrp();
    }

    public static XFrp getInstance() {
        return XFrpTypeClass.in;
    }

    private final String uuid = "c22a757a3ff0464bd6585c04b9e83a72";
    private boolean isRunning = false;

    private FrpListener frpListener;
    private FrpConfig frpConfig;

    public XFrp setFrpListener(FrpListener frpListener) {
        this.frpListener = frpListener;
        return this;
    }

    public boolean isRunning() {
        return Frpclib.isRunning(uuid);
    }

    private XFrp setConfig(FrpConfig frpConfig) {
        this.frpConfig = frpConfig;
        return this;
    }

    private void start() {
        if (isRunning) {
            return;
        }
        if (frpConfig == null){
            throw new NullPointerException("frpConfig == null");
        }
        new Thread(() -> {
            try {
                isRunning = true;
                Frpclib.runContent(uuid, frpConfig.toString());
            } catch (Throwable e) {
                e.printStackTrace();
            } finally {
                isRunning = false;
            }
        }).start();
    }
}
